package alex.rsx.http;

import alex.rsx.util.RsxUtil;
import android.util.Log;

import com.alex.http.AGetReuqest;
import com.alex.http.AHttpEngine;
import com.alex.http.AHttpListener;

/**
 * 
 * 服务提供
 * 
 * @author Alex.Lu
 *
 */
public class RsxHttpService {

	private final String TAG = "RsxHttpService";
	
	private final String ROOTURL = "http://www.rongshuxia.com/bookapi/";
	private final String URL_BOOK= "book/";
	private final String URL_DATA_JSON = "data-json-";
	private final String URL_FID_5 = "fid-5-";
	private final String URL_AUTH = "auth-";
	private final String URL_HTML = ".html";
	private final String URL_U = "-";
	public static final long HTTP_CMD_0 = 0;
	
	public static final long HTTP_INDEX = 0x0001;
	public static final long HTTP_CLASSFIY = 0x0002;
	public static final long HTTP_SUBJECT_LIST = 0x0003;
	public static final long HTTP_SUBJECT = 0x0004;
	public static final long HTTP_CHAPTER_LIST = 0x0005;
	public static final long HTTP_CHAPTER = 0x0006;
	public static final long HTTP_HIGHLY_RECOMMEND = 0x0007;
	public static final long HTTP_SEARCH = 0x0008;
	
	private static RsxHttpService instance;
	
	public static RsxHttpService getInstance(){
		if(instance == null){
			instance = new RsxHttpService();
		}
		return instance;
	}
	
	public void send(AHttpListener l){
		
	}
	
	/**
	 * 首页请求，推荐
	 * @param l
	 * @param start
	 * @param num
	 */
	public void sendReqeustIndexRec(AHttpListener l,int start,int num){
		StringBuffer sbuf = new StringBuffer();
		sbuf.append(ROOTURL);
		sbuf.append(URL_BOOK);
		sbuf.append(URL_DATA_JSON);
		sbuf.append(URL_FID_5);
		sbuf.append(URL_AUTH);
		sbuf.append("40644239");
		sbuf.append("-type-rec-");
		sbuf.append("start-");
		sbuf.append(start);
		sbuf.append(URL_U);
		sbuf.append("num-");
		sbuf.append(num);
		sbuf.append(URL_HTML);
		sendRequest(HTTP_INDEX,sbuf.toString(),l);
	}
	
	/**
	 * 首页请求，最新
	 * @param l
	 * @param start
	 * @param num
	 */
	public void sendReqeustIndexNew(AHttpListener l,int start,int num){
		StringBuffer sbuf = new StringBuffer();
		sbuf.append(ROOTURL);
		sbuf.append(URL_BOOK);
		sbuf.append(URL_DATA_JSON);
		sbuf.append(URL_FID_5);
		sbuf.append(URL_AUTH);
		sbuf.append("4f1f37ea");
		sbuf.append("-type-new-");
		sbuf.append("start-");
		sbuf.append(start);
		sbuf.append(URL_U);
		sbuf.append("num-");
		sbuf.append(num);
		sbuf.append(URL_HTML);
		sendRequest(HTTP_INDEX,sbuf.toString(),l);
	}
	
	/**
	 * 分类请求
	 * @param l
	 * @param type
	 * @param start
	 * @param num
	 */
	public void sendRequestClassify(AHttpListener l,int type,int start,int num){
		StringBuffer sbuf = new StringBuffer();
		sbuf.append(ROOTURL);
		sbuf.append("booklist/");
		sbuf.append(URL_DATA_JSON);
		sbuf.append(URL_FID_5);
		sbuf.append(URL_AUTH);
		sbuf.append(RsxUtil.getMD5_8("booklist"+type));
		sbuf.append(URL_U);
		sbuf.append("start-");
		sbuf.append(start);
		sbuf.append(URL_U);
		sbuf.append("num-");
		sbuf.append(num);
		sbuf.append(URL_HTML);
		sendRequest(HTTP_CLASSFIY,sbuf.toString(),l);
	}
	
	/**
	 * 获取专题列表
	 * @param l
	 */
	public void sendRequestSubjectList(AHttpListener l){
		StringBuffer sbuf = new StringBuffer();
		sbuf.append(ROOTURL);
		sbuf.append("booklist/");
		sbuf.append(URL_DATA_JSON);
		sbuf.append(URL_FID_5);
		sbuf.append(URL_AUTH);
		sbuf.append("54b3c3a5");
		sbuf.append(URL_HTML);
		sendRequest(HTTP_SUBJECT_LIST,sbuf.toString(),l);
	}
	
	/**
	 * 获取专题书籍列表
	 * @param l
	 * @param topid
	 * @param start
	 * @param num
	 */
	public void sendRequestSubject(AHttpListener l,int topid,int start,int num){
		StringBuffer sbuf = new StringBuffer();
		sbuf.append(ROOTURL);
		sbuf.append("topiclist/");
		sbuf.append(URL_DATA_JSON);
		sbuf.append(URL_FID_5);
		sbuf.append(URL_AUTH);
		sbuf.append(RsxUtil.getMD5_8("topiclist"+topid));
		sbuf.append(URL_U);
		sbuf.append("start-");
		sbuf.append(start);
		sbuf.append(URL_U);
		sbuf.append("num-");
		sbuf.append(num);
		sbuf.append(URL_HTML);
		sendRequest(HTTP_CLASSFIY,sbuf.toString(),l);
	}
	
	/**
	 * 获取章节列表
	 * @param l
	 * @param bookid
	 */
	public void sendRequestChapterList(AHttpListener l,int bookid){
		StringBuffer sbuf = new StringBuffer();
		sbuf.append(ROOTURL);
		sbuf.append("getchapter/");
		sbuf.append(URL_DATA_JSON);
		sbuf.append(URL_FID_5);
		sbuf.append(URL_AUTH);
		sbuf.append(RsxUtil.getMD5_8("getchapter"+bookid));
		sbuf.append(URL_HTML);
		sendRequest(HTTP_CHAPTER_LIST,sbuf.toString(),l);
	}
	
	/**
	 * 获取章节内容
	 * @param l
	 * @param chapterid
	 */
	public void sendRequestChapter(AHttpListener l,int chapterid){
		StringBuffer sbuf = new StringBuffer();
		sbuf.append(ROOTURL);
		sbuf.append("chapter/");
		sbuf.append(URL_DATA_JSON);
		sbuf.append(URL_FID_5);
		sbuf.append(URL_AUTH);
		sbuf.append(RsxUtil.getMD5_8("chapter"+chapterid));
		sbuf.append(URL_HTML);
		sendRequest(HTTP_CHAPTER,sbuf.toString(),l);
	}
	
	/**
	 * 获取强力推荐
	 */
	public void sendRequestHighlyRecommend(AHttpListener l){
		StringBuffer sbuf = new StringBuffer();
		sbuf.append(ROOTURL);
		sbuf.append("cover_rec/");
		sbuf.append(URL_DATA_JSON);
		sbuf.append(URL_FID_5);
		sbuf.append(URL_AUTH);
		sbuf.append("042165c7");
		sbuf.append(URL_HTML);
		sendRequest(HTTP_HIGHLY_RECOMMEND,sbuf.toString(),l);
	}
	
	//搜索结果
	public void sendRequestSreach(){
		
	}
	
	public void sendRequest(long cmd,String url,AHttpListener l){
		Log.e(TAG, "cmd:"+cmd+"  url:"+url);
		AGetReuqest request = new AGetReuqest(cmd,url,l,new IndexHandler(),null);
		AHttpEngine.getInstance().request(request);
	}
	
}
